package com.cnciems.lakekb.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnciems.lakekb.common.base.controller.BaseController;
import com.cnciems.lakekb.common.base.domain.AjaxResult;
import com.cnciems.lakekb.course.domain.KbCourse;
import com.cnciems.lakekb.course.service.ICourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @version V1.0
 * @program: lake-kb
 * @package: com.cnciems.lakekb.course
 * @description: 课程web层
 * @author: HHJ
 * @dep STATE GRID XIONG AN DIGITAL TECHNOLOGY Co.,Ltd
 * @create: 2021-05-24 10:41
 **/
@Api(tags = "课程管理")
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {

    @Autowired
    ICourseService courseService;

    @ApiOperation(value = "查询",notes = "查询")
    @GetMapping("/list")
    public AjaxResult list(){
        List<KbCourse> list = courseService.list();
        return success("成功",list);
    }

    @ApiOperation(value = "分页查询",notes = "分页查询课程列表")
    @GetMapping("/page")
    public AjaxResult page(){
        IPage<KbCourse> con = new Page<>(2,2);
        IPage<KbCourse> page = courseService.page(con);
        return Objects.isNull(page) ? error("获取失败") : success("成功",page);
    }

    @ApiOperation(value = "自定义分页查询",notes = "自定义分页查询课程列表")
    @GetMapping("/mypage")
    public AjaxResult mypage(){
        Page<KbCourse> con = new Page<>(2,2);
        IPage<KbCourse> page = courseService.selectPage(con,"否");
        return Objects.isNull(page) ? error("获取失败") : success("成功",page);
    }

    @ApiOperation(value = "根据ID查询",notes = "根据ID查询课程")
    @GetMapping("/get/{id}")
    public AjaxResult getById(@PathVariable("id") String id){
        KbCourse kbCourse = courseService.getById(id);
        return Objects.isNull(kbCourse) ? error("获取失败") : success("成功",kbCourse);
    }


    @ApiOperation(value = "插入")
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody KbCourse model){
        return courseService.save(model) ? success("插入成功"):error("插入失败");
    }
    @ApiOperation(value = "修改",notes = "修改课程信息")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody KbCourse model){
        return courseService.saveOrUpdate(model) ? success("修改成功"):error("修改失败");
    }
    @ApiOperation(value = "删除",notes = "根据ID删除课程")
    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id){
        return courseService.removeById(id) ? success("删除成功"):error("删除失败");
    }
}
